package drds.new_route;

import java.util.HashSet;
import java.util.List;

import drds.server.config.model.ColumnType;
import drds.server.config.model.Schema;
import drds.server.config.model.ShardKeyType;
import drds.server.config.model.Table;
import drds.server.sqlparser.parser.StatementParser;
import drds.server.sqlparser.sqlobject.expression.Expression;
import drds.server.sqlparser.sqlobject.statement.Statement;
import drds.server.sqlparser.sqlobject.statement.delete.DeleteStatement;
import drds.server.sqlparser.sqlobject.statement.insert.InsertStatement;
import drds.server.sqlparser.sqlobject.statement.insert.ValueList;
import drds.server.sqlparser.sqlobject.statement.update.UpdateSetItem;
import drds.server.sqlparser.sqlobject.statement.update.UpdateStatement;

public class Route {
	public static void main(String[] args) {
		String sql = null;
		StatementParser statementParser = new StatementParser(sql);
		Statement statement = statementParser.parseStatement();
		if (statement == null) {

		} else if (statement instanceof InsertStatement) {
			InsertStatement insertStatement = (InsertStatement) statement;
			String tableName = insertStatement.getTableName().getName().toString();
			List<Expression> columnNameList = insertStatement.getColumnNameList();
			HashSet<String> columnNameSet = insertStatement.getColumnNameSet();
			//
			Schema schema = null;
			Table table = schema.getTableMap().get(tableName);
			if (table == null) {
				throw new NullPointerException("table:tableName=" + tableName);
			}
			if (table.getTableType() == Table.TYPE_GLOBAL_TABLE && table.getShardkeyType() == ShardKeyType.NONE) {// 没有切分键
				// 直接写入到全局表
				// 全局自增
			} else if (table.getTableType() == Table.TYPE_NOT_GLOBAL_TABLE_AND_NOT_ER && table.getShardkeyType() == ShardKeyType.ONE_KEY) {
				if (table.isUseGlobalSequence()) {
					// (sequence_)
					if (table.isAutoSetSequenceNo()) {

					} else {
						// SQL替换
					}
					// String firstShardKeyName = table.getFirstShardKeyName();

				} else {

				}
			} else if (table.getTableType() == Table.TYPE_NOT_GLOBAL_TABLE_AND_NOT_ER && table.getShardkeyType() == ShardKeyType.DOUBLE_KEY) {
				if (table.isUseGlobalSequence()) {
					// (sequence_)
					if (table.isAutoSetSequenceNo()) {

					} else {
						// SQL替换
					}
					String firstShardKeyColumnName = table.getFirstShardKey().getColumnName();
					ColumnType firstShardKeyColumnType = table.getFirstShardKey().getColumnType();
					//
					String secondShardKeyColumnName = table.getSecondShardKey().getColumnName();
					ColumnType secondShardKeyColumnType = table.getSecondShardKey().getColumnType();
					// 一般不采用主键自增的模式进行填充
					if (!insertStatement.getColumnNameList().contains(firstShardKeyColumnName)) {
						throw new NullPointerException("firstShardKeyColumnName");
					}
					if (!insertStatement.getColumnNameList().contains(firstShardKeyColumnName)) {
						throw new NullPointerException("firstShardKeyColumnName");
					}
					// 取出对应的值
					ValueList valueList = insertStatement.getValueList();
					List<Expression> valueExpressionList = valueList.getValueList();
					Expression firstShardKeyValueExpression = valueExpressionList.get(insertStatement.getColumnNameIndexMap().get(firstShardKeyColumnName));
					Expression secondShardKeyValueExpression = valueExpressionList.get(insertStatement.getColumnNameIndexMap().get(secondShardKeyColumnName));
					// int long string date bigdecimal
					// 得到值带入切分算法得到具体的切分算法SD ST 以及对应的GroupId
					// 然后直接在master上面写入数据

				} else {

				}
			}

		} else if (statement instanceof UpdateStatement) {
			UpdateStatement updateStatement = (UpdateStatement) statement;
			String tableName = updateStatement.getTableName().getName().toString();
			// 切分条件不能更新
			List<UpdateSetItem> updateItemList = updateStatement.getUpdateItemList();
			// 需要判断where语句
			Expression where = updateStatement.getWhere();
			if (where == null) {
				throw new IllegalArgumentException("where");
			}
		} else if (statement instanceof DeleteStatement) {
			DeleteStatement deleteStatement = (DeleteStatement) statement;
			String tableName = deleteStatement.getTableName().getName().toString();
			// 切分条件不能更新
			// 需要判断where语句
			Expression where = deleteStatement.getWhere();
			if (where == null) {
				throw new IllegalArgumentException("where");
			}
			// 关键是如何从where语句里面获取切分条件。前提是需要判断当前的SQL语句在语法或者语义上面的正确性。

		}
	}

}
